package com.example.chapter07;

import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class UserLoginDemoServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        try {
            // 将表单数据填充到User对象中
            BeanUtils.populate(user, req.getParameterMap());
            // 将User对象设置到request作用域
            req.setAttribute("loginUser", user);
            // 请求转发到 loginResult.jsp 进行查询响应
            RequestDispatcher dispatcher = req.getRequestDispatcher("loginResult.jsp");
            dispatcher.forward(req, resp);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
